package com.example.rocketdemo.listener;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * @author GuoAo
 * @className LogTxMessageListener
 * @date 2025/7/16
 * @description TODO
 */
@Component
@Slf4j
@RocketMQTransactionListener
public class LogTxMessageListener implements RocketMQLocalTransactionListener {

    @Override
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object o) {
        log.info("事务消息：{}",message);
        log.info("Objects:{}",o);
        Object  payload = message.getPayload();
        log.info("txId:{}",message.getHeaders());
        // 不确定状态 触发 checkLocalTransaction方法回查
        return RocketMQLocalTransactionState.UNKNOWN;
    }

    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {
        log.info("事务回查：{}",message);
        log.info("事务回查：{}",message.getPayload());
        return RocketMQLocalTransactionState.COMMIT;
    }
}
